import requests
import json
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017/')
db_name = '中国地方政府债券'
client.drop_database(db_name)
db = client[db_name]
collection = db['data']
page = 1
for i in range(1, 1642):
    url = f"https://www.governbond.org.cn:4443/api/loadBondData.action?timeStamp=1744278770234&dataType=ZQFXLISTBYAD&adList=&adCode=87&zqlx=&year=&fxfs=&qxr=&fxqx=&zqCode=&zqName=&page={page}&pageSize=10"

    headers = {
        "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36"
    }

    response = requests.get(url, headers=headers)
    html = json.loads(response.text)
    data_to_insert = []
    for zq in html['data']:
        data_to_insert.append({
            "债券名称": zq['ZQ_NAME'],
            "债券编码": zq['ZQ_CODE'],
            "发行地区": zq['AD_NAME'],
            "发行时间": zq['ZQ_FXTIME'],
            "发行期限": zq['ZQQX_NAME'],
            "发行规模": zq['FX_AMT'],
            "利率": zq['LL'],
            "付息方式": zq['FXFS'],
            "起息日": zq['QX_DATE']
        })
        print(f"正在爬取第{page}页债券{zq['ZQ_CODE']}......")
    if data_to_insert:
        collection.insert_many(data_to_insert)
    page += 1

client.close()
